package com.um.pasantiaum.objetos;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class UserManager {

	private ArrayList<Usuario> mUsers;
	private static UserManager sUserManager;
	private static ParseUser sCurrentUser;

	public static UserManager getUserManager(ParseUser currentUser) {
		if (sUserManager == null || !currentUser.equals(sCurrentUser))
			sUserManager = new UserManager(currentUser);
		return sUserManager;
	}

	private UserManager(ParseUser currentUser) {
		mUsers = new ArrayList<Usuario>();
		sCurrentUser = currentUser;

		ParseQuery<ParseObject> query = ParseQuery.getQuery("UserEmpresa");
		query.whereEqualTo("empresa", sCurrentUser);
		query.whereEqualTo("isActive", true);
		query.include("user");

		try {
			List<ParseObject> objects = query.find();

			for (ParseObject po : objects) {
				ParseUser temp = po.getParseUser("user");
				mUsers.add(new Usuario(temp));
			}

		} catch (ParseException e1) {
			Log.d("Usuario", "Error: " + e1.getMessage());
		}
	}

	public void addUser(ParseUser us) {
		if (us != null) {
			ParseQuery<ParseObject> query = ParseQuery.getQuery("UserEmpresa");
			query.whereEqualTo("empresa", sCurrentUser);
			query.include("user");

			final ParseUser user = us;

			List<ParseObject> objects;
			try {
				objects = query.find();
				for (ParseObject o : objects) {
					ParseUser temp = o.getParseUser("user");
					if (temp.getObjectId().equals(user.getObjectId())
							&& !o.getBoolean("isActive")) {
						o.put("isActive", true);
						try {
							o.save();
							mUsers.add(new Usuario(temp));
						} catch (ParseException e1) {
							Log.d("User", "Error: " + e1.getMessage());
						}
						return;
					} else if (temp.getObjectId().equals(user.getObjectId())
							&& o.getBoolean("isActive")) {
						return;
					}
				}
				ParseObject temp = new ParseObject("UserEmpresa");
				temp.put("isActive", true);
				temp.put("empresa", sCurrentUser);
				temp.put("user", user);
				try {
					temp.save();
					mUsers.add(new Usuario(user));

				} catch (ParseException e1) {
					Log.d("User", "Error: " + e1.getMessage());
				}
			} catch (ParseException e) {
				Log.d("User", "Error: " + e.getMessage());
			}
		}
	}

	public void removeUser(Usuario us) {
		if (us != null){
			
			
			
			ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
			
			
			userQuery.getInBackground(us.getUserId(), new GetCallback<ParseUser>() {
				
				@Override
				public void done(ParseUser object, ParseException e) {
					if (e==null){
						ParseQuery<ParseObject> query = ParseQuery
								.getQuery("UserEmpresa");
						
						query.whereEqualTo("user", object);
						
						ParseObject aux;
						try {
							aux = query.getFirst();
							aux.put("isActive", false);
							aux.saveInBackground();
						} catch (ParseException e1) {
							Log.d("User", "Error: " + e1.getMessage());
						}						
					}else{
						Log.d("User", "Error: " + e.getMessage());
					}
					
				}
			});
			mUsers.remove(us);
		}

	}

	public ArrayList<Usuario> getUsers() {
		return mUsers;
	}

}
